/*
* Android平台下视频【直播】横竖屏自适应、模块切换等相关操作
* Author: Feil.Wang
* Date: 2012-10
**/


landscapeVariable = function(){
	bodyWidth = $(document.body).width();
	bodyHeight = $(document.body).height();
	smallDivHeight = (bodyHeight-114)/2;
	smallDivWidth = smallDivHeight*4/3;
}
portraitVariable = function(){
	bodyWidth = $(document.body).width();
	bodyHeight = $(document.body).height();
	smallDivWidth = (bodyWidth-8)/2;
	smallDivHeight = smallDivWidth*3/4;
}


/*
*横屏模式
*/
function AdaptiveLandscape(callback){
	$(".nipad").width(bodyWidth).height(bodyHeight);
	$(".rightTop").css({
		left:bodyWidth-smallDivWidth-3,
		top:54,
		height:smallDivHeight,
		width:smallDivWidth
	});
	$(".rightBottom").css({
		left:bodyWidth-smallDivWidth-3,
		top:smallDivHeight+54,
		width:smallDivWidth,
		height:smallDivHeight
	})
	$(".nipadMainL").css({
		left:5,
		top:54,
		height:bodyHeight-114,
		width:bodyWidth-smallDivWidth-6
	});
	$(".text-live-container,.qa-push-container").css({
		left:bodyWidth-smallDivWidth-318,
		top:bodyHeight-323
	});
	$(".pptCntM").height(bodyHeight-144);
	$("#videoWrap").height(smallDivHeight-30);
	$(".askCntM").height(smallDivHeight-132);
	callback();
}
/*
*竖屏模式
*/
function AdaptivePortrait(callback2){
	$(".nipad").width(bodyWidth).height(bodyHeight);
	$(".rightTop").css({
		left:5,
		top:54,
		width:smallDivWidth,
		height:smallDivHeight
	})
	$(".rightBottom").css({
		left:bodyWidth-smallDivWidth - 3,
		top:54,
		width:smallDivWidth,
		height:smallDivHeight
	});
	$(".nipadMainL").css({
		left:5,
		top:smallDivHeight + 55,
		width:bodyWidth-8,
		height:bodyHeight- smallDivHeight - 117
	});
	$(".text-live-container,.qa-push-container").css({
		left:bodyWidth-319,
		top:bodyHeight-325
	});
	$(".pptCntM").height(bodyHeight - smallDivHeight - 147);
	$("#videoWrap").height(smallDivHeight-30);
	$(".askCntM").height(smallDivHeight-132);
	callback2();
}
/*
*视频切换至主窗口
*/
function videoToMain(divToSmallA,divToSmallB){
	if (window.orientation == 90 || window.orientation == -90||window.orientation == undefined) {
		$(".rightTop").css({
			top:54,
			left:5,
			height:bodyHeight-114,
			width:bodyWidth-smallDivWidth-6
		}).addClass("divToMain");
		$(".nipadMainL,.rightBottom").removeClass("divToMain");
		$("#videoWrap").height(bodyHeight-144);
		divToSmallA();
		divToSmallB();
	}else if (window.orientation == 0 || window.orientation == 180) {
		$(".rightTop").css({
			top:smallDivHeight + 55,
			left:5,
			width:bodyWidth-8,
			height:bodyHeight- smallDivHeight - 117
		}).addClass("divToMain");
		$(".nipadMainL,.rightBottom").removeClass("divToMain");
		$("#videoWrap").height(bodyHeight- smallDivHeight - 117-30);
		divToSmallA();
		divToSmallB();
	}
}
/*
*视频切换至小窗口
*/
function videoToSmall(){
	if (window.orientation == 90 || window.orientation == -90||window.orientation == undefined) {
		$(".rightTop").css({
			left:bodyWidth-smallDivWidth-3,
			top:54,
			height:smallDivHeight,
			width:smallDivWidth
		});
		$("#videoWrap").height(smallDivHeight-30);	
	}else if (window.orientation == 0 || window.orientation == 180) {
		$(".rightTop").css({
			left:5,
			top:54,
			width:smallDivWidth,
			height:smallDivHeight
		});
		$("#videoWrap").height(smallDivHeight-30);
	}
}
/*
*视频切换至全屏
*/
function videoToFull(){
	if (window.orientation == 90 || window.orientation == -90||window.orientation == undefined) {
		$(".rightTop").css({
			left:-2,
			top:0,
			height:bodyHeight+4,
			width:bodyWidth+6
		}).addClass("divToFull");
		$("#videoWrap").height(bodyHeight-26);
	}else if(window.orientation == 0 || window.orientation == 180){
		$(".rightTop").css({
			left:-2,
			top:0,
			height:bodyHeight+4,
			width:bodyWidth+6
		}).addClass("divToFull");
		$("#videoWrap").height(bodyHeight-26);
	}
}
/*
*文档切换至主窗口
*/
function pptToMain(divToSmallA,divToSmallB){
	if (window.orientation == 90 || window.orientation == -90||window.orientation == undefined) {
		$(".nipadMainL").css({
			left:5,
			top:54,
			height:bodyHeight-114,
			width:bodyWidth-smallDivWidth-6
		}).addClass("divToMain");
		$(".rightTop,.rightBottom").removeClass("divToMain");
		$(".pptCntM").height(bodyHeight-144);
		divToSmallA();
		divToSmallB();
	}else if (window.orientation == 0 || window.orientation == 180) {
		$(".nipadMainL").css({
			left:5,
			top:smallDivHeight + 55,
			width:bodyWidth-8,
			height:bodyHeight- smallDivHeight - 117
		}).addClass("divToMain");
		$(".rightTop,.rightBottom").removeClass("divToMain");
		$(".pptCntM").height(bodyHeight - smallDivHeight - 147);
		divToSmallA();
		divToSmallB();
	}
}
/*
*文档切换至小窗口
*/
function pptToSmall(){
	if (window.orientation == 90 || window.orientation == -90||window.orientation == undefined) {
		$(".nipadMainL").css({
			left:bodyWidth-smallDivWidth-3,
			top:54,
			height:smallDivHeight,
			width:smallDivWidth
		});
		$(".pptCntM").height(smallDivHeight-30);
	}else if (window.orientation == 0 || window.orientation == 180) {
		$(".nipadMainL").css({
			left:5,
			top:54,
			width:smallDivWidth,
			height:smallDivHeight
		});
		$(".pptCntM").height(smallDivHeight-30);
		
	}
}
//文档切换至小窗口B
function pptToSmallB(){
	if (window.orientation == 90 || window.orientation == -90||window.orientation == undefined) {
		$(".nipadMainL").css({
			left:bodyWidth-smallDivWidth-3,
			top:smallDivHeight+54,
			width:smallDivWidth,
			height:smallDivHeight
		});
		$(".pptCntM").height(smallDivHeight-30);
	}else if (window.orientation == 0 || window.orientation == 180) {
		$(".nipadMainL").css({
			left:bodyWidth-smallDivWidth - 3,
			top:54,
			width:smallDivWidth,
			height:smallDivHeight
		});
		$(".pptCntM").height(smallDivHeight-30);
	}
}
/*
*文档切换至全屏
*/
function pptToFull(){
	if (window.orientation == 90 || window.orientation == -90||window.orientation == undefined) {
		$(".nipadMainL").css({
			left:-2,
			top:0,
			height:bodyHeight+4,
			width:bodyWidth+6
		}).addClass("divToFull");
		$(".pptCntM").height(bodyHeight-26);
	}else if(window.orientation == 0 || window.orientation == 180){
		$(".nipadMainL").css({
			left:-2,
			top:0,
			width:bodyWidth+6,
			height:bodyHeight
		}).addClass("divToFull");
		$(".pptCntM").height(bodyHeight - 26);
	}
}
/*
*问答切换至主窗口
*/
function askToMain(divToSmallA,divToSmallB){
	if (window.orientation == 90 || window.orientation == -90||window.orientation == undefined) {
		$(".rightBottom").css({
			left:5,
			top:54,
			height:bodyHeight-114,
			width:bodyWidth-smallDivWidth-6
		}).addClass("divToMain");
		$(".rightTop,.nipadMainL").removeClass("divToMain");
		$(".askCntM").height(bodyHeight-246);
		divToSmallA();
		divToSmallB();
	}else if (window.orientation == 0 || window.orientation == 180) {
		$(".rightBottom").css({
			left:5,
			top:smallDivHeight + 55,
			width:bodyWidth-8,
			height:bodyHeight- smallDivHeight - 117
		}).addClass("divToMain");
		$(".rightTop,.nipadMainL").removeClass("divToMain");	
		$(".askCntM").height(bodyHeight - smallDivHeight - 249);
		divToSmallA();
		divToSmallB();
	}
}
/*
*问答切换至小窗口
*/
function askToSmall(){
	if (window.orientation == 90 || window.orientation == -90||window.orientation == undefined) {
		$(".rightBottom").css({
			left:bodyWidth-smallDivWidth-3,
			top:smallDivHeight+54,
			width:smallDivWidth,
			height:smallDivHeight
		});
		$(".askCntM").height(smallDivHeight-132);
	}else if (window.orientation == 0 || window.orientation == 180) {
		$(".rightBottom").css({
			left:bodyWidth-smallDivWidth - 3,
			top:54,
			width:smallDivWidth,
			height:smallDivHeight
		});
		$(".askCntM").height(smallDivHeight-132);	
	}
}
/*
*窗口退出全屏
*/
function targetExitFull(){
	if($(".nipadMainL").hasClass("divToMain")){
		ppt2m();
		$(".nipadMainL,.rightTop").removeClass("divToFull");
	}else if($(".rightTop").hasClass("divToMain")){
		video2m();
		$(".nipadMainL,.rightTop").removeClass("divToFull");
	}else if($(".rightBottom").hasClass("divToMain")){
		ask2m();
		$(".nipadMainL,.rightTop").removeClass("divToFull");
	}
}
/*
*屏幕旋转
*/
function orient(){
	if (window.orientation == 90 || window.orientation == -90||window.orientation == undefined) {
		landscapeVariable();
		if($(".nipadMainL").hasClass("divToMain")&&!$(".nipadMainL,.rightTop,.rightBottom").hasClass("divToFull")){
			AdaptiveLandscape(ppt2m);
		}else if($(".rightTop").hasClass("divToMain")&&!$(".nipadMainL,.rightTop,.rightBottom").hasClass("divToFull")){
			AdaptiveLandscape(video2m);
		}else if($(".rightBottom").hasClass("divToMain")&&!$(".nipadMainL,.rightTop,.rightBottom").hasClass("divToFull")){
			AdaptiveLandscape(ask2m);
		}else if(!$(".nipadMainL").hasClass("divToMain")&&$(".nipadMainL").hasClass("divToFull")||$(".nipadMainL").hasClass("divToMain")&&$(".nipadMainL").hasClass("divToFull")){
			AdaptiveLandscape(pptToFull);
		}else if(!$(".rightTop").hasClass("divToMain")&&$(".rightTop").hasClass("divToFull")||$(".rightTop").hasClass("divToMain")&&$(".rightTop").hasClass("divToFull")){
			AdaptiveLandscape(videoToFull);
		}
	}else if (window.orientation == 0 || window.orientation == 180) {
		portraitVariable();
		if($(".nipadMainL").hasClass("divToMain")&&!$(".nipadMainL,.rightTop,.rightBottom").hasClass("divToFull")){
			AdaptivePortrait(ppt2m);
		}else if($(".rightTop").hasClass("divToMain")&&!$(".nipadMainL,.rightTop,.rightBottom").hasClass("divToFull")){
			AdaptivePortrait(video2m);
		}else if($(".rightBottom").hasClass("divToMain")&&!$(".nipadMainL,.rightTop,.rightBottom").hasClass("divToFull")){
			AdaptivePortrait(ask2m);
		}else if(!$(".nipadMainL").hasClass("divToMain")&&$(".nipadMainL").hasClass("divToFull")||$(".nipadMainL").hasClass("divToMain")&&$(".nipadMainL").hasClass("divToFull")){
			AdaptivePortrait(pptToFull);
		}else if(!$(".rightTop").hasClass("divToMain")&&$(".rightTop").hasClass("divToFull")||$(".rightTop").hasClass("divToMain")&&$(".rightTop").hasClass("divToFull")){
			AdaptivePortrait(videoToFull);
		}
	}
}
/*
*旋转时保持原有窗口大小和位置
*/
//ppt为主
function ppt2m(){
	pptToMain(videoToSmall,askToSmall);
}
//video为主
function video2m(){
	videoToMain(pptToSmall,askToSmall);
}
//ask为主
function ask2m(){
	askToMain(videoToSmall,pptToSmallB);
}

$(function() {		

	//初始化页面
	//ua=navigator.userAgent.toLowerCase();
	//if(ua.match(/android 2.3/i)=="android 2.3"){	//判断android版本
		setTimeout(function(){
			orient();
		},200);
	//}else{
	//	orient();
	//}
	//切换窗口
	$(".nipadb-a1").click(function(){
		video2m();
	});
	$(".nipadb-a2").click(function(){
		ppt2m();
	});
	$(".nipadb-a3").click(function(){
		ask2m();
	});
	$(".nipadMainL").toggle(function(){
		pptToFull();
	},function(){
		targetExitFull();
	});
	$("#videoWrap").click(function(){
		if($("#ctrlbar").hasClass("cHide")){
			$("#ctrlbar").removeClass("cHide");
		}else{
			$("#ctrlbar").addClass("cHide");
		}
	});
	$("#zoom").toggle(function(){
		videoToFull();
		ctrlBarHide();
		$(this).attr("class","z-small")
	},function(){
		targetExitFull();
		ctrlBarHide();
		$(this).attr("class","z-big")
	});
	//隐藏播放按钮及全屏按钮
	function ctrlBarHide(){
		$("#ctrlbar").addClass("cHide");
	}
	//问答Tabs切换
	$(".askTab li").eq(0).click(function(){
		$(".ask").removeClass("askCurrent2");
		$(".ask").addClass("askCurrent1")
	});
	$(".askTab li").eq(1).click(function(){
		$(".ask").removeClass("askCurrent1");
		$(".ask").addClass("askCurrent2")
	});
	//屏幕旋转事件绑定
	$(window).bind('orientationchange', function(e){
		//if(ua.match(/android 2.3/i)=="android 2.3"){
			setTimeout(function(){
				orient();
			},500);
		//}else{
		//	orient();
		//}
	});
	
	$("#play").click(function(event){
		event.stopPropagation();
		playPause();
		ctrlBarHide();
	});
	
});
function playPause(){
	var myVideo = document.getElementsByTagName('video')[0];
	if (myVideo.paused)
	myVideo.play();
	else
	myVideo.pause();
}
